package graphic.mycomponents;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class TextChecked extends JPanel implements KeyListener{

	public static int TEXT = 0;
	public static int PASSWORD = 1;
	public static int INT = 2;
	public static int FOLDER = 3;
	public static int WRITABLE_FOLDER = 4;
	public static int FILE = 5;
	
	protected static int CHAR_LIMIT = 30;
	
	int type;
	
	JTextField text;
	JLabel checkLabel;
	LinkedList<String> notEqual;
	
	JButton fileSelection;
	
	ImageIcon ok;
	ImageIcon notOk;
	
	boolean editable;
	boolean canBeNull;
	boolean checked;
	
	public TextChecked(String fieldText, int type, boolean editable, boolean canBeNull, LinkedList<String> notEqual)
	{
		super(new BorderLayout());
		
		this.notEqual = notEqual;
		
		this.notOk = new ImageIcon(TextChecked.class.getClassLoader().getResource("data/images/red.jpg"));
		this.ok = new ImageIcon(TextChecked.class.getClassLoader().getResource("data/images/green.jpg"));
		
		this.type = type;
		this.editable = editable;
		this.canBeNull = canBeNull;
		
		if(this.type == PASSWORD)
			this.text = new JPasswordField(fieldText);
		else
			this.text = new JTextField(fieldText);
			
		if((this.type == FILE) || (this.type == FOLDER) || (this.type == WRITABLE_FOLDER))
		{
			this.fileSelection = new JButton(new ImageIcon(TextChecked.class.getClassLoader().getResource("data/images/buttons/dir.jpg")));
			this.fileSelection.setOpaque(false);
			this.fileSelection.setBorder(null);
			this.fileSelection.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		
		if(this.type == FILE)
		{
			this.fileSelection.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent arg0)
				{
					JFileChooser chooser = new JFileChooser();
			        int returnVal = chooser.showOpenDialog(TextChecked.this);

			        if (returnVal == JFileChooser.APPROVE_OPTION)
			        	TextChecked.this.text.setText(chooser.getSelectedFile().getPath());
				}
			});
		} else if((this.type == FOLDER) || (this.type == WRITABLE_FOLDER))
		{
			this.fileSelection.addActionListener(new ActionListener()
			{
				@Override
				public void actionPerformed(ActionEvent act)
				{
					JFileChooser chooser = new JFileChooser();
					chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			        int returnVal = chooser.showOpenDialog(TextChecked.this);

			        if (returnVal == JFileChooser.APPROVE_OPTION)
			        {
			        	TextChecked.this.text.setText(chooser.getSelectedFile().getPath());
			        	TextChecked.this.check();
			        }
				}
			});
		}
		
		this.text.setEditable(this.editable);
		this.text.setOpaque(false);
		this.text.setBorder(null);
		this.text.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
		this.text.setForeground(Color.BLACK);
		this.text.addKeyListener(this);
		this.checkLabel = new JLabel(this.notOk);
		this.checkLabel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		
		if(type == FILE || type == FOLDER || type == WRITABLE_FOLDER)
		{
			JPanel panel = new JPanel(new BorderLayout(10, 0));
			panel.add(this.text, BorderLayout.CENTER);
			panel.add(this.fileSelection, BorderLayout.EAST);
			
			super.add(panel, BorderLayout.CENTER);
			super.add(this.checkLabel, BorderLayout.EAST);
		} else {
			super.add(this.text, BorderLayout.CENTER);
			super.add(this.checkLabel, BorderLayout.EAST);
		}
		
		this.check();
	}

	protected void setChecked(boolean chk)
	{
		if(chk)
			this.checkLabel.setIcon(this.ok);
		else
			this.checkLabel.setIcon(this.notOk);
		
		this.checked = chk; 
	}
	
	public String getText() {
		return this.text.getText().trim();
	}
	
	public void setText(String text){
		this.text.setText(text);
	}

	public boolean isChecked() {
		return checked;
	}

	protected void check()
	{
		String text = this.getText().trim();

		if(this.canBeNull)
			if(text.equals(""))
				{ this.setChecked(true); return; }
		
		if(text.equals("")){ this.setChecked(false); return; }
		
		if(this.type == INT)
			Integer.parseInt(text);
		
		if(this.type == TEXT || this.type == PASSWORD || this.type == INT)
			if(text.length() > TextChecked.CHAR_LIMIT){ this.setChecked(false); return; }
		
		if(this.type == TEXT || this.type == INT)
			if(text.split(" ").length != 1){ this.setChecked(false); return; }
		
		if(this.notEqual != null)
			if(this.notEqual.contains(text)){ this.setChecked(false); return; }
	
		if(this.type == FOLDER)
		{
			if(!new File(this.text.getText()).isDirectory())
				{ this.setChecked(false); System.out.println("ERR 1"); return; }
		}
		else if(this.type == WRITABLE_FOLDER)
		{
			try { 
				File temp = new File(this.text.getText(), "temp.tmp");
				temp.deleteOnExit();
				
				BufferedWriter out = new BufferedWriter(new FileWriter(temp)); 
				out.write("This is Just a Temp File!"); out.close();
				temp.delete();
				
			} catch (IOException e) { this.setChecked(false); return; }
		}
		else if(this.type == FILE)
		{
			File file = new File(this.text.getText());
			if(!file.canRead()) { this.setChecked(false); return; }
		}
		
		this.setChecked(true);
	}
	
	@Override
	public void keyPressed(KeyEvent e){}

	@Override
	public void keyReleased(KeyEvent e)
	{
		this.check();
	}

	@Override
	public void keyTyped(KeyEvent e) {}	
}